home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2712 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  128 lines

  1. Newsgroups: comp.lang.c
  2. Path: gail.ripco.com!mambuhl
  3. From: mambuhl@ripco.com (Martin Ambuhl)
  4. Subject: Re: directory functions .
  5. X-Nntp-Posting-Host: foley.ripco.com
  6. Message-ID: <DLMsxw.43u@rci.ripco.com>
  7. Sender: usenet@rci.ripco.com (Net News Admin)
  8. Organization: Ripco Internet BBS Chicago
  9. Date: Tue, 23 Jan 1996 11:22:43 GMT
  10. X-Ident-Sender: mambuhl
  11.  
  12. Hussein Salem <salem@ehvvs5.nwg.dec.com>
  13. in <30FF91CE.3F54@ehvvs5.nwg.dec.com> wrote:
  14.  
  15. >I'm trying to make a small program in C handling a directory. open it and print
  16. >it's content
  17. >:
  18. >The problem is it can't read it ! Why ? I 've set the mode to a+rwx to that dir
  19. >story !
  20. >These are the code and if you know what's the problem please tell me why ?
  21.  
  22. 1) Since dirent.h, sys/types.h, DIR *, opendir(), readdir() are none
  23. part of C, this is the wrong place for this (try comp.os.unix.*).
  24.  
  25. 2) If your implementation is ANSI compliant, "MYMAX" is wasted motion
  26. (try FILENAME_MAX)
  27.  
  28. 3) If your implementation is POSIX compliant, your definition of the
  29. struct you use for the return from readdir is wrong and unnecessary.
  30.  
  31. 4) There are several mis-parenthesized lines of code.
  32.  
  33. Your original code is at EOM.  A corrected version follows, but only
  34. because your code contains serious C errors.  If it had been only a
  35. question of your incorrect use of unix-isms, you should have had no
  36. expectation of help here:
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>             /* mha - added */
  40. #include <dirent.h>
  41. #if 0
  42. /* mha - removed */
  43. #include <sys/types.h>
  44. #define MYMAX   14
  45. #endif
  46.  
  47. #if 0
  48. /* mha - removed incorrect direct struct */
  49. typedef struct {
  50.     long ino;
  51.     char name[MYMAX + 1];
  52. }   Dirent;
  53. #endif
  54.  
  55. int main(int argc, char **argv)
  56. {                               /* mha - added explicit return type */
  57. #if 0
  58.     char name[FILENAME_MAX];    /* mha - was "MYMAX"; unused so removed */
  59. #endif
  60.     DIR *dfd;
  61.     char *dir;
  62.     struct dirent *dp;          /* mha - was "Dirent" */
  63.     dir = "/usr/users/salem/test";
  64.     if ((dfd = opendir(dir)) == NULL) { /* mha - fixed parens */
  65.         fprintf(stderr, " can't open %s\n", dir);
  66.         return EXIT_FAILURE;    /* mha - fixed return w/o value */
  67.     }
  68.     printf("dir opened %s\n", dir);
  69.     if ((dp = readdir(dfd)) == NULL) {
  70.         fprintf(stderr, " can't read %s\n", dir);
  71.         return EXIT_FAILURE;    /* mha - fixed return w/o value */
  72.     }
  73.     while ((dp = readdir(dfd)) != NULL) {
  74.         printf("name = %s\n", dp->d_name);  /* mha - was "dp->name".
  75.                                              * added '\n' */
  76.     }
  77.     closedir(dfd);
  78.     return 0;                   /* mha - added explicit return */
  79. }
  80.  
  81.  
  82. [ == salem's orginal code == ]
  83. >-----------------
  84.  
  85. >#include <stdio.h>
  86. >#include <dirent.h>
  87. >#include <sys/types.h>
  88. >#define MYMAX   14
  89.  
  90. >typedef struct {
  91. >        long ino;
  92. >        char name[MYMAX+1];
  93. >} Dirent;
  94.  
  95. >main(int argc,char **argv){
  96. >char name[MYMAX];
  97. >DIR     *dfd;
  98. >char *dir;
  99. >Dirent  *dp;
  100.  
  101. >dir = "/usr/users/salem/test";
  102.  
  103. >if ((dfd = opendir(dir) == NULL)) {
  104. >        fprintf(stderr," can't open %s\n",dir);
  105. >        return;
  106. >}
  107. >printf("dir opened %s\n",dir);
  108. >if ( (dp = readdir(dfd)) == NULL) {
  109. >fprintf(stderr," can't read %s\n",dir);return;
  110. >}
  111. >while ( (dp = readdir(dfd)) != NULL) {
  112. >        printf("name = %s",dp->name);
  113. >        /*if ( !strcmp(dp->name,argv[1]) || !strcmp(dp->name,"..") ) continue;
  114. >        printf("name = %s/%s \n",dir, dp->name); */
  115. >}
  116. >closedir(dfd);
  117.  
  118.  
  119. >}
  120.  
  121. Lose the 16-line .sig  If 4 lines aren't enough, then your ego is too
  122. big.
  123.  
  124.                                                               
  125. --
  126. * Martin Ambuhl       net: mambuhl@ripco.com
  127. * Chicago, IL (USA)    
  128.